<!DOCTYPE html>
<html>
<head>
    <title>PHP面向对象编程练习</title>
    <meta charset="utf-8">
</head>
<body>
<?php
// 定义一个类
/**
* 这是我的第一个类
*/
class ShopProduct {
    public $name = '';
    public $price = 0;
    private $cost = 0;
    protected $amount = 0;

    // 构造方法的名称为__construct，它通常用来初始化一个对象。
    // 构造方法会在对象被创建的时候自动运行。
    public function __construct($name = '',$price = 0,$cost=0,$amount=0)
    {
        $this->name   = $name;
        $this->price  = $price;
        $this->cost   = $cost;
        $this->amount = $amount;
    }

    public function display()
    {
        return $this->name . '的价格是：' . $this->price;
    }

    public function getCost()
    {
        return $this->cost;
    }

}

// 创建我的书类
/**
* 书籍类
*/
class Book extends ShopProduct
{
    public $author  = '';
    public $press   = '';
    public $pubdate = '';
    public $version = 1;
    
    function __construct($name = '',$price = 0,$cost=0,$amount=0,$author='',$press='',$pubdate='',$version=1 )
    {
        parent::__construct($name,$price,$cost,$amount);
        $this->author  = $author;
        $this->press   = $press;
        $this->pubdate = $pubdate;
        $this->version = $version;
    }

    // public function test()
    // {
    //     return parent::$amount;
    // }
}

// 创建一个类的实例
$p1 = new ShopProduct('999胃泰',9.9,0.9,999);

$p2 = new ShopProduct();

$book1 = new Book('PHP面向对象编程',29.9,10,15,'Mattow','NY press','2017-10',1);

echo '<pre>';
var_dump($p1->name,$p1->display(),$p1->getCost());
var_dump($book1->name,$book1->display(),$book1->getCost());
var_dump($book1->test());
echo '</pre>';


?>



</body>
</html>